import axios from 'axios';
import qs from 'qs';
import { HOST } from './config';

const service = axios.create({
    timeout: 30000
});

service.interceptors.response.use(response => {
    let res = {
        status: response.data.status,
        msg: response.data.msg,
        data: response.data.data ? JSON.parse(response.data.data) : []
    };
    return res;
}, error => {
    if (error.message.includes('timeout')) {
        alert('网络超时');
    }
    if (error.message.includes('Network Error')) {
        alert('网络错误');
    }
    return Promise.reject(error);
});

export function post (url, data = {}) {
    return new Promise((resolve, reject) => {
        let sendObject = {
            url: HOST + url,
            method: 'POST',
            data: qs.stringify(data)
        };
        service(sendObject).then(res => {
            resolve(res);
        }).catch(error => {
            reject(error);
        });
    });
}
